﻿@using Microsoft.AspNetCore.Components
@using System.ComponentModel.DataAnnotations
@namespace MudBlazor.Docs.Examples

<MudDropContainer T="KanbanTaskItem" @ref="_dropContainer" Items="@_tasks" ItemsSelector="@((item,column) => item.Status == column)" ItemDropped="TaskUpdated" Class="d-flex flex-row">
	<ChildContent>
		@foreach (var item in _sections)
		{
			<MudPaper Elevation="0" Width="224px" MinHeight="400px" Class="pa-4 ma-4 d-flex flex-column mud-background-gray rounded-lg">
				<MudToolBar Gutters="false">
					<MudText Typo="Typo.subtitle1"><b>@item.Name</b></MudText>
					<MudSpacer />
					<MudMenu Icon="@Icons.Material.Rounded.MoreHoriz" AnchorOrigin="Origin.BottomRight" TransformOrigin="Origin.TopRight" ListClass="pa-2 d-flex flex-column" PopoverClass="mud-elevation-25">
						<MudButton Size="Size.Small" Color="Color.Error" StartIcon="@Icons.Material.Outlined.Delete" OnClick="@( () => DeleteSection(item))">Delete section</MudButton>
						<MudButton Size="Size.Small" Color="Color.Default" StartIcon="@Icons.Material.Rounded.Edit">Rename section</MudButton>
					</MudMenu>
				</MudToolBar>
				<MudDropZone T="KanbanTaskItem" Identifier="@item.Name" Class="mud-height-full" />
				@if (item.NewTaskOpen)
				{
					<MudPaper Elevation="25" Class="pa-2 rounded-lg">
						<MudTextField @bind-Value="item.NewTaskName" Placeholder="New Task" Underline="false" Margin="Margin.Dense" Class="mx-2 mt-n2"></MudTextField>
						<MudButton OnClick="@(() => AddTask(item))" Size="Size.Small" Color="Color.Primary" FullWidth="true">Add task</MudButton>
					</MudPaper>
				}
				else
				{
					<MudButton OnClick="@(() => item.NewTaskOpen = !item.NewTaskOpen)" StartIcon="@Icons.Material.Filled.Add" FullWidth="true" Class="rounded-lg py-2">Add task</MudButton>
				}
			</MudPaper>
		}
		<MudPaper Class="pa-4" Elevation="0" Width="224px">
			@if (_addSectionOpen)
			{
				<MudPaper Elevation="0" Width="224px" Class="pa-4 d-flex flex-column mud-background-gray rounded-lg">
					<EditForm Model="@newSectionModel" OnValidSubmit="OnValidSectionSubmit">
						<DataAnnotationsValidator />
						<MudTextField @bind-Value="newSectionModel.Name" For="@(() => newSectionModel.Name)" Placeholder="New section" Underline="false"></MudTextField>
						<MudButton ButtonType="ButtonType.Submit" Size="Size.Small" Color="Color.Primary" FullWidth="true">Add section</MudButton>
					</EditForm>
				</MudPaper>
			}
			else
			{
				<MudButton OnClick="OpenAddNewSection" Variant="Variant.Outlined" StartIcon="@Icons.Material.Filled.Add" Color="Color.Primary" Class="rounded-lg py-2" FullWidth="true">Add section</MudButton>
			}
		</MudPaper>
	</ChildContent>
	<ItemRenderer>
		<MudPaper Elevation="25" Class="pa-4 rounded-lg my-3">@context.Name</MudPaper>
	</ItemRenderer>
</MudDropContainer>

@code {
	private MudDropContainer<KanbanTaskItem> _dropContainer;

	private bool _addSectionOpen;
	/* handling board events */
	private void TaskUpdated(MudItemDropInfo<KanbanTaskItem> info)
	{
		info.Item.Status = info.DropzoneIdentifier;
	}

	/* Setup for board  */
	private List<KanBanSections> _sections = new()
		{
			new KanBanSections("To Do", false, String.Empty),
			new KanBanSections("In Process", false, String.Empty),
			new KanBanSections("Done", false, String.Empty),
		};

	public class KanBanSections
	{
		public string Name { get; init; }
		public bool NewTaskOpen { get; set; }
		public string NewTaskName { get; set; }

		public KanBanSections(string name, bool newTaskOpen, string newTaskName)
		{
			Name = name;
			NewTaskOpen = newTaskOpen;
			NewTaskName = newTaskName;
		}
	}
	public class KanbanTaskItem
	{
		public string Name { get; init; }
		public string Status { get; set; }

		public KanbanTaskItem(string name, string status)
		{
			Name = name;
			Status = status;
		}
	}

	private List<KanbanTaskItem> _tasks = new()
		{
			new KanbanTaskItem("Write unit test", "To Do"),
			new KanbanTaskItem("Some docu stuff", "To Do"),
			new KanbanTaskItem("Walking the dog", "To Do"),
		};

	KanBanNewForm newSectionModel = new KanBanNewForm();

	public class KanBanNewForm
	{
		[Required]
		[StringLength(10, ErrorMessage = "Name length can't be more than 10.")]
		public string Name { get; set; }
	}

	private void OnValidSectionSubmit(EditContext context)
	{
		_sections.Add(new KanBanSections(newSectionModel.Name, false, String.Empty));
		newSectionModel.Name = string.Empty;
		_addSectionOpen = false;
	}

	private void OpenAddNewSection()
	{
		_addSectionOpen = true;
	}

	private void AddTask(KanBanSections section)
	{
		_tasks.Add(new KanbanTaskItem(section.NewTaskName, section.Name));
		section.NewTaskName = string.Empty;
		section.NewTaskOpen = false;
		_dropContainer.Refresh();
	}

	private void DeleteSection(KanBanSections section)
	{
		if (_sections.Count == 1)
		{
			_tasks.Clear();
			_sections.Clear();
		}
		else
		{
			int newIndex = _sections.IndexOf(section) - 1;
			if (newIndex < 0)
			{
				newIndex = 0;
			}

			_sections.Remove(section);

			var tasks = _tasks.Where(x => x.Status == section.Name);
			foreach (var item in tasks)
			{
				item.Status = _sections[newIndex].Name;
			}
		}
	}
}
